<template>
  <el-dialog :visible="showdialog"  title="添加房间" @close="btnCancel">
    <el-form :model="roomform" :rules="rules" ref="roomsForm">
       <el-form-item label="房间名" label-width="100px" prop="name">
         <el-input v-model="roomform.name" style="width:80%"></el-input>
      </el-form-item>
      <el-form-item label="房间号" label-width="100px"  prop="num">
         <el-input v-model.number="roomform.num" style="width:80%"></el-input>
      </el-form-item>
       <el-form-item label="价格" label-width="100px" prop="price">
         <el-input v-model.number="roomform.price" style="width:80%"></el-input>
      </el-form-item>
      <el-form-item label="类型" label-width="100px" prop="type">    
           <el-select v-model.number="roomform.type">
          <el-option
          v-for="item in roomTypes"
          :value="item.id"
          :key="item.id"
          :label="item.value"
         />
           </el-select>
      </el-form-item>
      <el-form-item label="状态" label-width="100px" prop="state" >
          <el-select v-model.number="roomform.state">
          <el-option
           v-show="item.id>0"
          v-for="item in roomState"
          :value="item.id"
          :key="item.id"
          :label="item.value"
         />
           </el-select>
      </el-form-item>
      <el-form-item label="简介" label-width="100px" prop="baseinfo">
         <el-input type="textarea" v-model="roomform.baseinfo" style="width:80%" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import Rooms from '@/utils/constant/rooms'
import {addRoom,getRoomList } from '@/api/rooms'
export default {
props:{
  showdialog:{
    type:Boolean,
    default:false
  }
},
data(){
  // 校验房间号是否存在
 const checkNum=async(rule, value, callback)=>{
    const rst=await getRoomList()
    const flag=rst.some(item=>item.num===value)
    return flag?callback(new Error('该房间号已存在')):callback()
    }
    // 校验房间名是否存在
    const checkname=async(rule, value, callback)=>{
    const rst=await getRoomList()
    const flag=rst.some(item=>item.name===value)
    return flag?callback(new Error('该房间号已存在')):callback()
    }
  return {
    roomTypes:Rooms.type,
    roomState:Rooms.state,
    roomform:{
      num:null,
      name:null,
      price:null,
      type:null,
      state:null,
      baseinfo:null,
      image:[],
      income:0,
      time:0,
      repair:{
        expenditure:0,
        reason:null
      }
    },
    rules:{
      num:[ { required: true, message: '请输入房间号', trigger: 'blur' },
      { type: 'number', message: '请输入正确的房间号', trigger: 'blur' },
      {validator:checkNum,trigger:'blur'}
      ],
      name:[{ required: true, message: '请输入房间名', trigger: 'blur' },
       {validator:checkname,trigger:'blur'}],
      price:[{ required: true, message: '请输入房间价格', trigger: 'blur' },
      { type: 'number', message: '请输入正确的价格', trigger: 'blur' }],
      type:[{ required: true, message: '请选择房间类型', trigger: 'blur' },
      { type: 'number', message: '请输入正确的类型', trigger: 'blur' }],
      state:[{ required: true, message: '请选择房间状态', trigger: 'blur' },
      { type: 'number', message: '请选择正确的状态', trigger: 'blur' }],
      baseinfo:[{ required: true, message: '请填写房间基本信息', trigger: 'blur' }]
    }
  }
},
methods:{
  async btnOK(){
      // 确认添加
      await this.$refs.roomsForm.validate()
      await addRoom(this.roomform)
      this.$message.success('添加房间成功')
      this.btnCancel()
      this.$parent.getRoomList()
    },
    btnCancel(){
      // 删除
      this.$refs.roomsForm.resetFields()
      this.$parent.showdialog=false
    }
  }
}
</script>

<style>

</style>